<?php
namespace app\test01\controller;

use app\common\controller\Base as CommonBase;
use app\common\exception\ForbidenException;
use app\common\model\SystemConfig as SystemConfigModel;
use think\App;
use think\facade\Session;
use app\common\model\Auth as AuthModel;
use app\common\model\ManagerRole as ManagerRoleModel;
use app\common\model\Role as RoleModel;
use app\common\model\RoleAuth as RoleAuthModel;
use think\facade\Env;

class BaseAdmin extends CommonBase
{
    //当前的模块
    protected $current_module = 'manage';

    //当前的控制器
    protected $current_controller = '';

    //当前的方法
    protected $current_action = '';


    protected $breads = array();
    public $bodyData = array();

    public function __construct(App $app = null)
    {
        // var_dump($_SERVER['HTTP_HOST']);
        $envDomain=Env::get('environment.manage');
        if($envDomain&&$envDomain!=$_SERVER['HTTP_HOST']){
            exit("您迷路了".$_SERVER['HTTP_HOST']);
        }
        parent::__construct($app);
        $prebody = $_POST;
		foreach ($prebody as $k => $v) {
            if(is_string($v)){
                $this->bodyData[$k] = htmlspecialchars($v);
            }else{
                $this->bodyData[$k] = ($v);
            }
           
        }
		$prebody = $_GET;
		foreach ($prebody as $k => $v) {
            $this->bodyData[$k] = htmlspecialchars($v);
        }
		$prebody = file_get_contents("php://input");
		if($prebody&&json_decode($prebody)){
			$prebody=json_decode($prebody,true);		
		}else{
			$prebody=[];
		}
		foreach ($prebody as $k => $v) {
			
            $this->bodyData[$k] = htmlspecialchars($v);
        }
        $system_configs = $this->getSystemConfig();
        $this->assign('configs', $system_configs);



        $this->current_module = $this->request->module();
        $this->current_controller = $this->request->controller();
        $this->current_action = $this->request->action();




        $manager = Session::get('manager');
		
        if (empty($manager) && (!in_array($this->current_controller, ['Login', 'Upload']))) {
            $this->redirect(url('Login/login'));
        }


        if (in_array($this->current_action, array('map')) || in_array($this->current_controller, array('Login', 'Index', 'Upload')) || $manager->is_system == 1 || $manager->is_developer == 1) {
           //某些控制器不需要权限，
            //系统管理员拥有一切的权限
        } else {
            $role_ids = ManagerRoleModel::where('type_id', $manager->manager_id)->where('module_name', $this->current_module)->where('type_type', $this->current_module)->column('role_id');

            if (count($role_ids) <= 0) {
                die('很抱歉，您还没有相应的权限');
            }
            $auth_ids = RoleAuthModel::where('role_id', 'in', implode(',', $role_ids))->where('module_name', $this->current_module)->column('auth_id');
            if (count($auth_ids) <= 0) {
                die('很抱歉，您还没有相应的权限');
            }

            $auth = AuthModel::where('auth_id', 'in', implode(',', $auth_ids))->where('module_name', '=', $this->current_module)->where('controller_name', '=', $this->current_controller)->where('action_name', '=', $this->current_action)->find();
            if (empty($auth)) {
                if ($this->request->isAjax()) {
                    throw new ForbidenException('很抱歉，你没有相应的权限');
                } else {
                    die('很抱歉，您没有相应的权限');
                }
            }
            if (empty($auth)) {
                if ($this->request->isAjax()) {
                    throw new ForbidenException('很抱歉，你没有相应的权限');
                } else {
                    die('很抱歉，您没有相应的权限');
                }
            }
        }

        //
        $this->breads[] = array(
            'name' => '',
            'url'  => ''
        );


    }
}